package samber

import (
	"fmt"
	"github.com/samber/lo"
	"math"
	"strconv"
)

// RandomString 随机指定位数
func RandomString() {
	result := lo.RandomString(5, lo.LettersCharset)
	fmt.Printf("%v \n", result) // XVlBz
}

// Range 按顺序个数Range
func Range() {
	result := lo.Range(4)
	fmt.Printf("%v \n", result) // [0 1 2 3]
}

func RangeFrom() {
	result := lo.RangeFrom(1, 5)
	fmt.Printf("%v \n", result) // [1 2 3 4 5]
}

func RangeWithSteps() {
	result := lo.RangeWithSteps(0, 20, 5)
	fmt.Printf("%v \n", result) // [0 5 10 15]
}

// Reduce 数组累计结果
func Reduce() {
	list := []int64{1, 2, 3, 4}
	result := lo.Reduce(list, func(ago int64, item int64, index int) int64 {
		return ago + item
	}, 1)
	fmt.Printf("%v \n", result) // 11
}

// ReduceRight 从右边开始数组累计
func ReduceRight() {
	list := [][]int{{0, 1}, {2, 3}, {4, 5}}

	result := lo.ReduceRight(list, func(agg []int, item []int, index int) []int {
		return append(agg, item...)
	}, []int{})
	fmt.Printf("%v \n", result) // [4 5 2 3 0 1]
}

// Reject Filter 相反
func Reject() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.Reject(list, func(x int, _ int) bool {
		return x%2 == 0
	})
	fmt.Printf("%v \n", result) // [1 3 5]
}

// Repeat 重复N个初始化的值
func Repeat() {
	result := lo.Repeat[foo](2, foo{bar: "2"})
	fmt.Printf("%v \n", result) // [{ } { }]
}

// RepeatBy 按照自己的逻辑Repeat
func RepeatBy() {
	result := lo.RepeatBy[string](5, func(i int) string {
		return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
	})
	fmt.Printf("%v \n", result) // [0 1 4 9 16]
}

// Replace 返回的是副本 替换逻辑跟 .replace类似
func Replace() {
	list := []int{0, 1, 0, 1, 2, 3, 0}

	result := lo.Replace(list, 0, 42, 1)
	fmt.Printf("%v\n", result) // [42 1 0 1 2 3 0]
}

// ReplaceAll 返回的是副本 符合的都替换
func ReplaceAll() {
	list := []int{0, 1, 0, 1, 2, 3, 0}

	result := lo.ReplaceAll(list, 0, 42) // [42 1 42 1 2 3 42]
	fmt.Printf("%v\n", result)
}

// Reverse 倒过来
func Reverse() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.Reverse(list)
	fmt.Printf("%v\n", result) // [5 4 3 2 1 0]
}

// RuneLength 用Rune获取长度
func RuneLength() {
	result1, chars1 := lo.RuneLength("hellô"), len("hellô")
	result2, chars2 := lo.RuneLength("🤘"), len("🤘")

	fmt.Printf("%v %v\n", result1, chars1) // 5 6
	fmt.Printf("%v %v\n", result2, chars2) // 1 4
}
